package com.example.testdemo.demos.web.Controller;

import com.example.testdemo.demos.web.Config.JwtInfo;
import com.example.testdemo.demos.web.Config.Result;
import com.example.testdemo.demos.web.Entity.ShippingAddress;
import com.example.testdemo.demos.web.Service.JwtRedistService;
import com.example.testdemo.demos.web.Service.ShippingAddressService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * 用户的收货地址类
 */
@RestController
@RequestMapping("/address")
public class ShippingAddressController {

    @Resource
    private ShippingAddressService addressService;

    @Resource
    private JwtRedistService jwtRedistService;


    /**
     * 新增用户收货地址
     * @param address
     * @param token
     * @return
     */
    @PostMapping("/addAddress")
    public Result postAddAddress(@RequestBody ShippingAddress address, @RequestHeader("token") String token){
        JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
        if(jwtInfo.getPass()){
            address.setUserId(jwtInfo.getUserId());
            return addressService.addAddress(address);
        }else {
            return jwtInfo.getResult();
        }
    }

    /**
     * 删除用户收货地址
     * @param address
     * @param token
     * @return
     */
    @PostMapping("/delAddress")
    public Result postDelAddress(@RequestBody ShippingAddress address, @RequestHeader("token") String token){
        JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
        if(jwtInfo.getPass()){
            address.setUserId(jwtInfo.getUserId());
            return addressService.delAddress(address);
        }else {
            return jwtInfo.getResult();
        }
    }

    /**
     * 查询用户收货地址
     * @param token
     * @return
     */
    @GetMapping("/selAddress")
    public Result postDelAddress(@RequestHeader("token") String token){
        JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
        if(jwtInfo.getPass()){
            ShippingAddress address =new ShippingAddress();
            address.setUserId(jwtInfo.getUserId());
            return addressService.selAddress(address);
        }else {
            return jwtInfo.getResult();
        }
    }

    /**
     * 更新用户收货地址
     * @param address
     * @param token
     * @return
     */
    @PostMapping("/setAddress")
    public Result postSetAddress(@RequestBody ShippingAddress address, @RequestHeader("token") String token){
        JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
        if(jwtInfo.getPass()){
            address.setUserId(jwtInfo.getUserId());
            return addressService.setAddress(address);
        }else {
            return jwtInfo.getResult();
        }
    }


}
